package com.atguigu.tingshu.album.receive;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: jack
 * @date: 2025/9/29 20:25
 */
@Component
@Slf4j
public class AlbumReceive {


    @Autowired
    private AlbumInfoService albumInfoService;

    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(MqConst.EXCHANGE_TRACK),
            value = @Queue(MqConst.QUEUE_TRACK_STAT_UPDATE),
            key = MqConst.ROUTING_TRACK_STAT_UPDATE
    ))
    @GuiGuCache
    public void updateTrackStat(TrackStatMqVo trackStatMqVo, Message message, Channel channel) {
        //1 .使用redis判断是否播放的数据
        log.info("接收到消息：{}", trackStatMqVo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        albumInfoService.updateTrackStat(trackStatMqVo);
    }
}
